<?php
declare(strict_types=1);

namespace App\Cache;

use Carbon\Carbon;

/**
 * 命名规范：{业务}:{数据类型}:{标识}:{子标识}
 */
class CacheKey
{
    // ---------------- 签到相关 -------------------
    public static function signinMonth(int $userId, int $year, int $month): string
    {
        return sprintf('signin:bitmap:%d:%d_%d', $userId, $year, $month);
    }

    public static function signinGlobal(string $date): string
    {
        return sprintf('signin:global:%s', $date);
    }

    public static function continuousDays(int $userId): string
    {
        return sprintf('signin:continuous:%d', $userId);
    }

    public static function signinTotalDay(int $userId): string
    {
        return sprintf('signin:total:%d', $userId);
    }

    // ---------------- 缓存相关 -------------------
    public static function userInfo(int $userId): string
    {
        return sprintf('user:info:hash:%d', $userId);
    }

    public static function articleInfo(int $articleId): string
    {
        return sprintf('article:info:hash:%d', $articleId);
    }

    // 文章敏感词扫描结果
    public static function articleSensitiveWords(int $articleId): string
    {
        return sprintf('article:sensitive:hash:%d', $articleId);
    }

    // 带有敏感词的文章id集合
    public static function haveSensitiveWordArticles(): string
    {
        return 'article:sensitive:set';
    }

    // 审核带有内容争议的文章结果
    public static function reviewControversialResult(int $articleId): string
    {
        return sprintf('article:controversial:hash:%d', $articleId);
    }

    public static function hotArticle(string $date): string
    {
        return sprintf('article:hot:string:%s', $date);
    }

    public static function articleView(int $articleId): string
    {
        return sprintf('article:view:string:%d', $articleId);
    }

    // 当更新了用户的密码、状态、角色之后，iat在此时间点之前的token需要全部取消
    public static function forbidTokenBefore(int $uid): string
    {
        return sprintf('forbid:token:before:string:%d', $uid);
    }

    public static function activeUser(): string
    {
        return 'active:user:zset';
    }

    public static function activeIp(): string
    {
        return 'active:ip:zset';
    }

    // -------------------- 会话和锁相关 -----------------
    public static function sessionKey(string $sessionId): string
    {
        return sprintf('session:%s', $sessionId);
    }

    public static function requestCountMinute(): string
    {
        return 'request:count:minute:zset';
    }

    public static function exceptionCountMinute(): string
    {
        return 'exception:count:minute:zset';
    }

    // 记录文章的浏览次数，每天每个用户只计算一次，如果没有登录，则根据ip判断，每个ip计算一次
    public static function userViewArticle(int $articleId, Carbon $date): string
    {
        return sprintf('article:view:today:set:%d:%s', $articleId, $date->format('Ymd'));
    }
}